package com.software.levelupandroid.systemTask.view.activity;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.loopeer.cardstack.CardStackView;
import com.software.levelupandroid.R;
import com.software.levelupandroid.entity.Resource;
import com.software.levelupandroid.entity.SystemTask;
import com.software.levelupandroid.entity.Task;
import com.software.levelupandroid.systemTask.model.SystemShowViewModel;
import com.software.levelupandroid.systemTask.presenter.TaskPresenter;
import com.software.levelupandroid.systemTask.view.IView;
import com.software.levelupandroid.systemTask.view.activity.FirstSelectionActivity;
import com.software.levelupandroid.systemTask.view.adapter.TestStackAdapter;
import com.software.levelupandroid.taskShow.TaskShowActivity;
import com.software.levelupandroid.taskShow.viewModel.TaskShowViewModel;
import com.software.levelupandroid.utils.DateUtil;
import com.software.levelupandroid.utils.ToastUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SecondSelectionActivity extends AppCompatActivity implements IView{
    private Toolbar toolbar;
    private CardStackView cardStackView;
    private TestStackAdapter mTestStackAdapter;
    List<String> list = new ArrayList<String>();
    private Integer[] TEST_DATA;
    private TaskPresenter taskPresenter;
    private SystemShowViewModel systemShowViewModel;
    String type = null;


    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_selection);


        InitData();

        toolbar = findViewById(R.id.toolbar_second);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null)
            actionBar.setDisplayShowTitleEnabled(false);

        taskPresenter = new TaskPresenter(this);
        cardStackView = (CardStackView) findViewById(R.id.stackview_main);

        new Handler().postDelayed(
                new Runnable() {
                    @Override
                    public void run() {
                        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                        mTestStackAdapter =
                                new TestStackAdapter(SecondSelectionActivity.this, list, alarmManager, taskPresenter,Integer.parseInt(type));
                        cardStackView.setAdapter(mTestStackAdapter);
                        mTestStackAdapter.updateData(Arrays.asList(TEST_DATA));
                    }
                }
                , 200
        );

    }


    private void InitData() {
        Intent intent = getIntent();
        type = intent.getStringExtra("type");
        initViewModel();
        systemShowViewModel.getSystemTaskListByType(SecondSelectionActivity.this,Integer.parseInt(type));
        observeLiveData();
        //Toast.makeText(this, type, Toast.LENGTH_SHORT).show();
    }

    private void initViewModel() {
        ViewModelProvider viewModelProvider = new ViewModelProvider(this);
        systemShowViewModel = viewModelProvider.get(SystemShowViewModel.class);
    }
    private void observeLiveData() {
        systemShowViewModel.getSystemTaskListLiveData().observe(this, new Observer<Resource<List<SystemTask>>>() {
            @Override
            public void onChanged(Resource<List<SystemTask>> listResource) {

                if (listResource != null){
                    if (listResource.isSuccess()){
                        List<SystemTask> data = listResource.getData();
                        TEST_DATA = new Integer[data.size()];
                        if (data != null){
                            for (int i = 0; i < data.size(); i++){
                                //添加系统任务标题
                               list.add(data.get(i).getTitle());
                               //添加卡片颜色
                               int color = Integer.parseInt(data.get(i).getContent());
                               TEST_DATA[i] = color;
                            }
                        }
                        System.out.println("systemAll"+ data.toString());
                    }else if (listResource.isFailure()){
                        ToastUtil.showToastOnUI(SecondSelectionActivity.this, listResource.getErrorMessage());
                    } else if (listResource.isLoading()) {

                    }
                }
            }
        });
    }

    /**
     * IView接口中新增任务成功的方法
     * @param task
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void issueTaskSuccess(SystemTask task) {
        if (task.getStatus() == 1 && task.getTitle() != null){
            Long aLong = DateUtil.convertStringToLong(task.getTime(), "yyyy年MM月dd日 HH:mm");
            //设置提醒
//            Intent intent = new Intent(this, NotionService.class);
//            PendingIntent service = PendingIntent.getService(this, 0, intent, 0);
//            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//            alarmManager.set(AlarmManager.RTC_WAKEUP, aLong, service);
        }


        ToastUtil.showToastOnUI(SecondSelectionActivity.this, "新增任务成功");


        finish();
    }

    /**
     * IView接口中新增任务失败的方法
     * @param msg
     */
    @Override
    public void issueTaskFailure(String msg) {

        ToastUtil.showToastOnUI(SecondSelectionActivity.this, "新增任务失败");

        Log.e("error", msg);
    }

    /**
     * IView接口中删除任务成功
     * @param task
     */
    @Override
    public void deleteTaskSuccess(SystemTask task) {
        finish();
        ToastUtil.showToastOnUI(SecondSelectionActivity.this, "删除任务成功");
    }

    /**
     * IView接口中删除任务失败
     * @param msg
     */
    @Override
    public void deleteTaskFailure(String msg) {
        ToastUtil.showToastOnUI(SecondSelectionActivity.this, msg);
    }

    /**
     * IView接口中任务状态更新成功
     * @param task
     */
    @Override
    public void isFinishSuccess(SystemTask task) {
        ToastUtil.showToastOnUI(SecondSelectionActivity.this, "任务状态更新成功");
    }

    /**
     * IView接口中任务状态更新失败
     * @param msg
     */
    @Override
    public void isFinishFailure(String msg) {
        ToastUtil.showToastOnUI(SecondSelectionActivity.this, msg);
    }


    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return true;
    }
}